import HourModel from "../../schema/hour";
import { Request, Response } from "express";
import type Typings from "../../typings";

// Common 类的实例
import { Common, ResStatus } from "../common/common";
const CommonIns = new Common(HourModel);
const ResStatusIns = new ResStatus();

class Hour {
  constructor() {}

  // 列表数据
  async list(req: Request, res: Response) {
    try {
      const creatorId = CommonIns.getLoginUserId(
        req as Typings.Express.ConvertRequest
      );

      let { page = 1, size = 10, startTime, endTime } = req.body;
      page = Number(page);
      size = Number(size);

      const query: Partial<Typings.Hour.props> = {
        creatorId,
      };

      if (startTime && endTime) {
        query.date = { $gte: Number(startTime), $lte: Number(endTime) };
      }

      const total = await CommonIns.Count(query);

      const list: Typings.Hour.props[] = await CommonIns.Find(query)
        .skip(size * (page - 1))
        .limit(size)
        .sort({
          createdTime: -1,
        });

      const data = {
        page,
        size,
        list,
        total,
      };

      ResStatusIns.Success(res, data);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 添加
  async add(req: Request, res: Response) {
    try {
      const creatorId = CommonIns.getLoginUserId(
        req as Typings.Express.ConvertRequest
      );

      const { date, content } = req.body;

      const hour: Typings.Hour.props[] = await CommonIns.Find({
        date,
      });

      if (hour.length) {
        ResStatusIns.isExist(res);
        return;
      }

      await CommonIns.SaveOne({
        date,
        content,
        creatorId: creatorId,
        createdTime: Date.now(),
        updatedTime: Date.now(),
      });

      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 更新
  async update(req: Request, res: Response) {
    try {
      const { id, date, content } = <Partial<Typings.Hour.props>>req.body;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      const updateContent = {
        date,
        content,
        updatedTime: Date.now(),
      };

      await CommonIns.UpdateOne({ id }, updateContent);
      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 查看
  async check(req: Request, res: Response) {
    try {
      const { id } = req.query;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      // 查询用户数据
      const hour: Typings.Hour.props[] = await CommonIns.Find({
        id,
      });

      if (!hour.length) {
        // 用户不存在
        ResStatusIns.isNotExist(res);
        return;
      }

      // 用户存在时
      ResStatusIns.Success(res, hour[0]);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 删除
  async remove(req: Request, res: Response) {
    try {
      let { id } = req.query;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      // 删除用户数据
      await CommonIns.RemoveOne({ id });

      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }
}

export default Hour;
